/*
 * visitorApp.cpp
 *
 *  Created on: Jul 18, 2013
 *      Author: m
 */

#include <iostream>
#include <cstdlib>
#include <ctime>
#include <sstream>
#include "Visitor.h"
#include "BusStationAPI.h"
#include "EntranceDoorAPI.h"
#include "BusAPI.h"

#define ARG_VISITORNAME 1

int main ( int argc, char* argv[] )
{
    if ( argc != 2 )
    {
        std::cerr << "Cantidad de argumentos invalida." << std::endl;
        return 1;
    }

    srand (time(NULL));

    std::string visitorName( argv[ARG_VISITORNAME] );

    std::clog << "Visitor Name: " << visitorName << std::endl;

    application::Visitor visitor( visitorName );


    application::EntranceDoorAPI entranceDoorApi;

    std::vector<application::DoorPass> entranceList = entranceDoorApi.getAvaliaveDoors();

    application::BusStationAPI busStationApi( visitorName );

    application::BusAPI busApi;

    try
    {
        int choice = rand() % entranceList.size();

        choice = 0;

        application::DoorPass entrance = entranceList.at( choice );

        std::clog << "Chosen Door: " << entrance.m_doorName << std::endl;

        entranceDoorApi.enterStub( entrance, visitor );

        application::Ticket ticket = visitor.getTicket();


        std::clog << "Waiting bus in: " << ticket.m_entranceBusStationPass.m_busStationName
                  << std::endl;

        busStationApi.waitEntranceBus( visitor );

        std::clog << "Taking Bus" << std::endl;

        bool traveledToPark = busApi.takeEntranceBus( visitor );

        if ( traveledToPark )
        {
            std::clog << "I'm in the park!" << std::endl;
        }
        else
        {
            throw Exception( "They didn't let me reach the park :(" );
        }

        // Acá debería elegir una puerta de salida, por el momento voy a hardcodear una
        //  al elegir la puerta se debería setear tmb el busStationPass para llegar a esa puerta
        //  (esto tmb lo voy a hardcodear)

        visitor.getTicket().m_exitPass.m_doorName = "salidaUnica";
        visitor.getTicket().m_exitPass.m_doorAddress = "none";

        visitor.getTicket().m_exitBusStationPass.m_busStationName = "busStation2";
        visitor.getTicket().m_exitBusStationPass.m_busStationAddress = BUS_STATION_REQUEST_BASENAME
                + std::string("busStation2");

        // DEBUG!
        std::string exitParkCommand;
        std::cout << "Press ENTER to leave the park" << std::endl;
        std::cin >> exitParkCommand;

        busStationApi.waitExitBus( visitor );

        bool traveledToExit = busApi.takeExitBus( visitor );

        if ( traveledToExit )
        {
            std::clog << "I left the park, GOODBYE!" << std::endl;
        }
        else
        {
            throw Exception( "They didn't let me reach the exit, I'm TRAPPED in the park!!!!" );
        }
    }
    catch( Exception &e )
    {
        if ( std::string( e.what() ).compare("Invalid Ticket") == 0 )
        {
            std::clog << "I have an invalid ticket!" << std::endl;
        }
        else
        {
            throw e;
        }
    }

    return 0;
}
